<template>
  <div class="register-container">
    <div class="register-content">
      <div class="logo-section">
        <div class="logo">📱 抖音</div>
        <div class="tagline">加入我们，记录美好生活</div>
      </div>

      <div class="form-section">
        <div class="form-group">
          <input v-model="form.username" type="email" placeholder="请输入邮箱" class="input-field" @blur="validateEmail">
          <div v-if="emailError" class="error-tip">{{ emailError }}</div>
        </div>

        <div class="form-group password-group">
          <input v-model="form.password" :type="showPassword ? 'text' : 'password'" placeholder="请输入密码 (至少6位)"
            class="input-field" @input="checkPasswordStrength">
          <span class="password-toggle" @click="showPassword = !showPassword">
            {{ showPassword ? '🙈' : '👁️' }}
          </span>
        </div>

        <div v-if="form.password" class="password-strength">
          <div class="strength-bar">
            <div class="strength-fill" :class="passwordStrength.class" :style="{ width: passwordStrength.width }"></div>
          </div>
          <div class="strength-text">{{ passwordStrength.text }}</div>
        </div>

        <div class="form-group">
          <input v-model="form.confirmPassword" type="password" placeholder="请确认密码" class="input-field"
            @blur="validateConfirmPassword">
          <div v-if="confirmPasswordError" class="error-tip">{{ confirmPasswordError }}</div>
        </div>

        <div class="agreement">
          <label class="checkbox-wrapper">
            <input type="checkbox" v-model="agreedToTerms">
            <span class="checkmark"></span>
            我已阅读并同意抖音的服务条款
          </label>
        </div>

        <button @click="registerAction" :disabled="!canRegister || loading" class="register-btn">
          {{ loading ? '注册中...' : '注册' }}
        </button>

        <div class="login-link">
          已有账号？
          <span @click="$router.push('/login')" class="link-text">
            登录
          </span>
        </div>
      </div>
    </div>

    <div class="footer">
      <p>注册即表示同意抖音的服务条款</p>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  name: 'Register',
  data() {
    return {
      form: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      showPassword: false,
      loading: false,
      agreedToTerms: false,
      emailError: '',
      confirmPasswordError: ''
    }
  },
  computed: {
    canRegister() {
      return this.form.username &&
        this.form.password &&
        this.form.confirmPassword &&
        this.agreedToTerms &&
        !this.emailError &&
        !this.confirmPasswordError &&
        this.form.password.length >= 6
    },
    passwordStrength() {
      const password = this.form.password
      if (password.length < 6) {
        return { text: '密码强度：弱', class: 'weak', width: '33%' }
      } else if (password.length < 10) {
        return { text: '密码强度：中', class: 'medium', width: '66%' }
      } else {
        return { text: '密码强度：强', class: 'strong', width: '100%' }
      }
    }
  },
  created() {
    if (this.$store.getters.isLoggedIn) {
      this.$router.push('/feed')
    }
  },
  methods: {
    ...mapActions(['register']),

    validateEmail() {
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
      if (this.form.username && !emailRegex.test(this.form.username)) {
        this.emailError = '请输入有效的邮箱地址'
      } else {
        this.emailError = ''
      }
    },

    validateConfirmPassword() {
      if (this.form.confirmPassword && this.form.password !== this.form.confirmPassword) {
        this.confirmPasswordError = '两次输入的密码不一致'
      } else {
        this.confirmPasswordError = ''
      }
    },

    checkPasswordStrength() {
      this.validateConfirmPassword()
    },

    async registerAction() {
      if (!this.canRegister) {
        this.$message.warning('请完善注册信息')
        return
      }

      this.loading = true

      try {
        await this.register({
          username: this.form.username,
          password: this.form.password
        })

        this.$message.success('注册成功!')
        this.$router.push('/login')
      } catch (error) {
        console.error('注册失败', error)
        this.$message.error('注册失败: ' + (error.response?.data?.message || error.message))
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.register-container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #000 0%, #333 100%);
  color: #fff;
  position: relative;
  overflow: hidden;
}

.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at 20% 50%, rgba(255, 45, 146, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(255, 107, 53, 0.1) 0%, transparent 50%);
  pointer-events: none;
}

.register-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px 30px;
  position: relative;
  z-index: 1;
  overflow-y: auto;
}

.logo-section {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  font-size: 48px;
  font-weight: bold;
  margin-bottom: 15px;
  background: linear-gradient(135deg, #ff2d92, #ff6b35);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.tagline {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.8);
  font-weight: 300;
}

.form-section {
  width: 100%;
  max-width: 320px;
}

.form-group {
  margin-bottom: 20px;
  position: relative;
}

.password-group {
  position: relative;
}

.input-field {
  width: 100%;
  padding: 15px 20px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 25px;
  color: #fff;
  font-size: 16px;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.input-field:focus {
  outline: none;
  border-color: #ff2d92;
  box-shadow: 0 0 20px rgba(255, 45, 146, 0.3);
  background: rgba(255, 255, 255, 0.15);
}

.input-field::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.password-toggle {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  font-size: 20px;
  user-select: none;
  transition: all 0.3s ease;
}

.password-toggle:hover {
  transform: translateY(-50%) scale(1.1);
}

.error-tip {
  color: #ff4757;
  font-size: 12px;
  margin-top: 5px;
  padding-left: 10px;
}

.password-strength {
  margin-top: 10px;
  margin-bottom: 10px;
}

.strength-bar {
  width: 100%;
  height: 4px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 5px;
}

.strength-fill {
  height: 100%;
  border-radius: 2px;
  transition: all 0.3s ease;
}

.strength-fill.weak {
  background: #ff4757;
}

.strength-fill.medium {
  background: #ffa502;
}

.strength-fill.strong {
  background: #2ed573;
}

.strength-text {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
  text-align: center;
}

.agreement {
  margin: 25px 0;
}

.checkbox-wrapper {
  display: flex;
  align-items: center;
  cursor: pointer;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
}

.checkbox-wrapper input[type="checkbox"] {
  display: none;
}

.checkmark {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 4px;
  margin-right: 10px;
  position: relative;
  transition: all 0.3s ease;
}

.checkbox-wrapper input[type="checkbox"]:checked+.checkmark {
  background: #ff2d92;
  border-color: #ff2d92;
}

.checkbox-wrapper input[type="checkbox"]:checked+.checkmark::after {
  content: '✓';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  font-size: 12px;
}

.register-btn {
  width: 100%;
  padding: 15px 20px;
  background: linear-gradient(135deg, #ff2d92, #ff6b35);
  color: #fff;
  border: none;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin: 20px 0;
  box-shadow: 0 4px 15px rgba(255, 45, 146, 0.3);
}

.register-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(255, 45, 146, 0.4);
}

.register-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.login-link {
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
}

.link-text {
  color: #ff2d92;
  cursor: pointer;
  font-weight: 500;
  text-decoration: none;
  transition: all 0.3s ease;
}

.link-text:hover {
  color: #ff6b35;
  text-decoration: underline;
}

.footer {
  padding: 20px;
  text-align: center;
  color: rgba(255, 255, 255, 0.5);
  font-size: 12px;
  position: relative;
  z-index: 1;
}

/* 移动端适配 */
@media (max-width: 480px) {
  .register-content {
    padding: 30px 20px;
  }

  .logo {
    font-size: 40px;
  }

  .form-section {
    max-width: 280px;
  }

  .input-field {
    padding: 12px 16px;
    font-size: 14px;
  }
}

/* 动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.form-section {
  animation: fadeInUp 0.6s ease-out;
}

.logo-section {
  animation: fadeInUp 0.6s ease-out 0.2s both;
}
</style>